﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using FifthSpaceModel;

namespace FifthSpaceDAL
{
    public class UserService
    {


        //新用户注册
        public bool AddUser(User user)
        {
            //string sql="insert into User values(@UserID,@IdentityId,@UserName,@Password,@Address,@Telephone)";
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO [UserID]([IdentityId],[UserName],[Password],[Address],[Telephone])");
            sb.Append(" VALUES(@UserID,@IdentityId,@UserName,@Password,@Address,@Telephone)");
            string sql = sb.ToString();
            sql += " ; SELECT @@IDENTITY";
            SqlParameter[] paras = new SqlParameter[]
           {
               new SqlParameter("@UserID",user.UserID),
               new SqlParameter("@IdentityId",user.IdentityId),
               new SqlParameter("@UserName",user.UserName),
               new SqlParameter("@Address",user.Address),
               new SqlParameter("@Telephone",user.Telephone),
               new SqlParameter("@Password",user.Password)           
           };

            return Convert.ToInt32(SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, CommandType.Text, sql, paras)) > 0;
        }

        public User UserLoginByUserId(string UserId)
        {
            string sql = "SELECT [LoginId] ,[LoginPwd] FROM [Users] where [LoginId]=@LoginId";
            User user = new User();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnString))
            {
                using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnString, CommandType.Text, sql, new SqlParameter("@UserId", UserId)))
                {
                    if (reader.Read())
                    {
                        user.UserID = (string)reader["UserId"];
                        user.Password = (string)reader["Password"];
                    }
                }
            }
            return user;
        }
    }
}
